package com.crm.action;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.crm.easyui.Grid;
import com.crm.po.Users;
import com.crm.service.UserService;
import com.crm.util.ResponseUtils;
import com.crm.util.SESSION;

public class UserAction extends Servlet {

	private static final long serialVersionUID = 1116994082953057010L;
	
	@SuppressWarnings("unchecked")
	public void getUsersList(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		UserService service = new UserService();
		Map<String, Object> map = service.getUsers(request);
		
		Grid grid = new Grid();
		grid.setTotal((Integer)map.get("count"));
		grid.setRows((List<Users>)map.get("rows"));
		
		ResponseUtils.printJson(response, null, grid, null,null);
	}
	
	public void saveOrUpdateUsers(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		UserService service = new UserService();

		int result = service.saveOrUpdateUsers(request);
		String value = "";
		if(result == 1){
			value = "ok";
		}else if(result == 0){
			value = "新增或修改用户失败，请检查您的输入数据是否有效。";
		}else if(result == -1){
			value = "已存在相同用户名称的用户，请重新输入用户名称。";
		}else{
			value = "新增或修改用户失败，系统出现异常。";
		}
		ResponseUtils.printString(response, value);
	}
	
	public void deleteUsers(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		UserService service = new UserService();

		boolean result = service.deleteUsers(request);
		
		String value = result ? "ok": "删除用户失败，请检查您的系统是否出现错误。";
		ResponseUtils.printString(response, value);
	}
	
	public void getUsersById(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		UserService service = new UserService();

		Users users = service.getUsersById(request);
		if(users != null){
			ResponseUtils.printJson(response, null, users, null, null);
		}else{
			ResponseUtils.printString(response, "获取用户信息失败，请检查您的系统是否出现错误。");
		}
	}
	
	public void updateSelfInfo(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		UserService service = new UserService();

		boolean result = service.updateSelfInfo(request);
		
		String value = result ? "ok": "修改个人信息失败，请检查您的系统是否出现错误。";
		ResponseUtils.printString(response, value);
	}
	
	public void logout(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.getSession(true).setAttribute(SESSION.USER_SESSION_KEY, null);
		request.getSession(true).invalidate();
	}
	
	public void openUsers(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		UserService service = new UserService();

		boolean result = service.openUsers(request);
		
		String value = result ? "ok": "启用用户失败，请检查您的系统是否出现错误。";
		ResponseUtils.printString(response, value);
	}
	
	public void closeUsers(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		UserService service = new UserService();

		boolean result = service.closeUsers(request);
		
		String value = result ? "ok": "禁用用户失败，请检查您的系统是否出现错误。";
		ResponseUtils.printString(response, value);
	}
	
}
